package com.neuedu.nevserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.nevserver.pojo.FarmBatch;
import com.neuedu.nevserver.service.FarmBatchService;
import com.neuedu.nevserver.service.ProductTypeService;
import com.neuedu.nevserver.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjy
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/farm-batch")
@Api(tags="养殖企业批号管理",description = "提供养殖企业批号管理API接口")
public class FarmBatchController {

    @Autowired
    private FarmBatchService farmBatchService;

    @Autowired
    private ProductTypeService productTypeService;

    @PostMapping("/addFarmBatch")
    @ApiOperation("新增养殖企业批号")
    public ResultVO addFarmBatch(FarmBatch farmBatch) throws Exception{
        farmBatch.setBatchDate(new Date());
        farmBatchService.save(farmBatch);
        return ResultVO.success("新增成功");
    }

    @GetMapping("/listFarmBatch")
    @ApiOperation("查询养殖企业产品批号列表")
    public ResultVO<List<FarmBatch>> listFarmBatch(FarmBatch farmBatch) throws Exception{
        QueryWrapper qw = new QueryWrapper();
        if(farmBatch.getFbId()!=null){
            qw.eq("fb_id", farmBatch.getFbId());
        }
        if(farmBatch.getBatchId()!=null && !farmBatch.getBatchId().equals("")){
            qw.eq("batch_id", farmBatch.getBatchId());
        }
        if(farmBatch.getNodeId()!=null){
            qw.eq("node_id", farmBatch.getNodeId());
        }
        if(farmBatch.getType()!=null){
            qw.eq("type", farmBatch.getType());
        }
        if(farmBatch.getState()!=null) {
            qw.eq("state", farmBatch.getState());
        }
        List<FarmBatch> farmBatchlist = farmBatchService.list(qw);
        for(FarmBatch fb:farmBatchlist){
            fb.setProductType(productTypeService.getById(fb.getType()));
        }
        return ResultVO.success(null,farmBatchlist);
    }

    @GetMapping("/updateBatchState")
    @ApiOperation("")
    public ResultVO updateBatchState(Integer fbId, Integer state) throws Exception{
        UpdateWrapper uw = new UpdateWrapper<>();
        uw.set("state", state);
        uw.eq("fb_id", fbId);
        farmBatchService.update(uw);
        return ResultVO.success("操作成功");
    }
}
